<?php
if(!defined('AB_TABLE_PREFIX'))define('AB_TABLE_PREFIX', '', true);
abstract class ARecord{
	protected $a_table = array();
	protected $o_dbconnect;
	protected $a_cols = array();
	protected $s_cond = '';
	protected $s_orderby = 1;
	protected $s_limit = '';
	protected $s_groupBy = '';
	protected $a_values = array();

	function __construct(){
		$this->o_dbconnect = dbConnect::getInstance();
	}

	function cols(){
		$a_args = func_get_args();
		$this->a_cols= array_map(array($this, 'forCols'),$a_args);
		return $this;
	}

	function setValue(){
		$a_args = func_get_args();
		$this->a_values = ABApi::params($a_args);
	}

	function tables(){
		$a_args  = func_get_args();
		$this->a_table=array_map(array($this, 'addPrefix'), $a_args);
		return $this;
	}
	
	function order($s_orderby){
		$this->s_orderby = $s_orderby;
		return $this;
	}
	
	function groupBy($s_gb){
		$s_gb = $this->forCols($s_gb);
		$this->s_groupBy = "GROUP BY $s_gb";
		return $this;
	}

	function limit($i_total, $i_init = null){
		$s_tmp= $i_init === null? ' ':sprintf(' %u, ', $i_init);
		$this->s_limit = sprintf("LIMIT$s_tmp%u", $i_total);
		return $this;
	}

	function getFields($s_table){
		return $this->o_dbconnect->getFields($s_table);
	}

	function where(){
		$a_args = func_get_args();
		if (count($a_args) < 2){
			$this->s_cond = " WHERE {$a_args[0]} ";
		}else{
			$s_str = array_shift($a_args);
			$a_args = array_map(array($this->o_dbconnect,'safe'), $a_args);
			array_unshift($a_args, $s_str);
			$s_str = ' WHERE ' . call_user_func_array('sprintf', $a_args);
			$this->s_cond = str_replace('#@',AB_TABLE_PREFIX, $s_str); #agrega los prefijos
			
		}
		return $this;
	}

	function select(){
		$s_field = implode(', ', $this->a_cols);
		$s_table = implode(' JOIN ', $this->a_table);
		$s_sql = "SELECT $s_field FROM $s_table {$this->s_cond}  {$this->s_groupBy} ORDER BY {$this->s_orderby} {$this->s_limit}";
		ABApi::trace($s_sql);
		$this->s_groupBy = '';
		$this->s_orderby = '1';
		return $this->o_dbconnect->query($s_sql);
	}

	function select_one(){
		$this->s_limit = 'LIMIT 1';
		$a_ob = $this->select();
		$this->limit = '';
		return isset($a_ob[0])? $a_ob[0]  :null;
	}

	function lastid($s_id){
		$this->cols($s_id);
		$this->order("$s_id DESC");
		return $this->select_one()->$s_id;
	}

	function insert(){
		$s_field = implode(', ', $this->a_cols);
		$s_table = implode(', ', $this->a_table);
		$s_value = implode(', ', $this->valForInsert());
		$s_sql = "INSERT INTO $s_table ($s_field) VALUES ($s_value)";
		return $this->o_dbconnect->query($s_sql);
	}

	function update($s_idfield, $i_id){
		$s_field = implode(',', $this->valForUpdate());
		$s_table = implode(', ', $this->a_table);
		$s_field = str_replace('%', '%%', $s_field);
		$s_sql = sprintf("UPDATE $s_table SET $s_field WHERE %.30s = %u", $this->safe($s_idfield), $i_id);
		ABApi::trace($s_sql);
		return $this->o_dbconnect->query($s_sql);
	}

	function delete($s_idfield, $i_id){
		$s_table = implode(', ', $this->a_table);
		$s_sql = sprintf("DELETE FROM $s_table WHERE %.30s = %u {$this->s_limit}", $this->safe($s_idfield), $i_id);
		ABApi::trace($s_sql);
		return $this->o_dbconnect->query($s_sql);
	}

	protected function addPrefix($s_str){
		return AB_TABLE_PREFIX.$s_str;
	}
	
	protected function forCols($s_str){
		$s_str=(strpos($s_str, '.')!==false)?$this->addPrefix($s_str):$s_str;
		return $this->safe($s_str);
	}
	
	
	protected function valForInsert(){
		$a_tmp = array();
		foreach($this->a_cols as $s_val){
			$a_tmp[] = isset($_POST[$s_val])?'"'.$this->safe($_POST[$s_val]).'"':'"'.$this->a_values[$s_val].'"';
		}
		return $a_tmp;
	}
	
	protected function valForUpdate(){
		$a_tmp = array();
		foreach($this->a_cols as $s_val){
			$a_tmp[] = isset($_POST[$s_val])?"$s_val = \"".$this->safe($_POST[$s_val]).'"':"$s_val = \"".$this->a_values[$s_val].'"';
		}
		return $a_tmp;
	}
	
	protected function safe($s_str){
		return $this->o_dbconnect->safe($s_str);
	}
}
?>
